/**
 *
 */
package jp.seraph.same.view.cui;

import jp.seraph.cui.AbstractMode;
import jp.seraph.cui.InternalConsole;
import jp.seraph.cui.Mode;
import jp.seraph.cui.SystemNode;
import jp.seraph.cui.SystemNodeType;
import jp.seraph.jsade.model.AngleVelocityCalculator;
import jp.seraph.jsade.model.JointIdentifierContainer;
import jp.seraph.jspf.pose.PoseManager;
import jp.seraph.same.controller.DefaultMotionSessionController;
import jp.seraph.same.controller.MotionController;
import jp.seraph.same.controller.MotionSessionController;
import jp.seraph.same.model.MotionModel;
import jp.seraph.same.model.MotionSessionModel;

/**
 *
 *
 */
public class MotionData extends AbstractMode {

    /**
     * @param aParent
     * @param aConsole
     */
    public MotionData(MotionEditMode aParent, InternalConsole aConsole, String aMotionName, MotionModel aModel, MotionController aController, PoseManager aPoseManager, JointIdentifierContainer aIdContainer, AngleVelocityCalculator aDefaultCalculator) {
        super(aParent, aConsole);
        mMotionName = aMotionName;
        mModel = aModel;
        mController = aController;
        mPoseManager = aPoseManager;
        mDefaultCalculator = aDefaultCalculator;
        mIdContainer = aIdContainer;
    }

    private String mMotionName;

    private MotionModel mModel;

    private MotionController mController;

    private PoseManager mPoseManager;

    private JointIdentifierContainer mIdContainer;

    private AngleVelocityCalculator mDefaultCalculator;

    /**
     *
     * @see jp.seraph.cui.SystemNode#getName()
     */
    public String getName() {
        return mMotionName;
    }

    /**
     *
     * @see jp.seraph.cui.SystemNode#getType()
     */
    public SystemNodeType getType() {
        return MotionDataType.getInstance();
    }

    /**
     *
     * @see jp.seraph.cui.SystemNode#isRoot()
     */
    public boolean isRoot() {
        return false;
    }

    /**
     *
     * @see jp.seraph.cui.SystemNode#setName(java.lang.String)
     */
    public void setName(String aName) {
        mMotionName = aName;
    }

    public void createSession(int aIndex) {
        addSession();
        int tLastIndex = mModel.getChildCount() - 1;

        for (int i = tLastIndex; i >= aIndex; i--) {
            swapChild(i, i - 1);
            mController.swapSession(i, i - 1);
        }
    }

    private void swapChild(int aLeft, int aRight) {
        SystemNode tLeft = this.getChild(Integer.toString(aLeft));
        SystemNode tRight = this.getChild(Integer.toString(aRight));

        String tTemp = tLeft.getName();
        tLeft.setName(tRight.getName());
        tRight.setName(tTemp);
    }

    public void addSession() {
        int tCount = mModel.getChildCount();
        Mode tParent = this;

        MotionSessionModel tModel = mController.createSession(tCount, "");
        MotionSessionController tController = new DefaultMotionSessionController(tModel, mController, mPoseManager, mIdContainer, mDefaultCalculator);
        MotionSessionData tNewElement = new MotionSessionData(tParent, this.getConsole(), Integer.valueOf(tCount), tModel, tController, mIdContainer);

        tParent.addChild(tNewElement);
    }

    public MotionModel getMotionModel(){
        return mModel;
    }

    @Override
    public void delete() {
        super.delete();
        mController.delete();
    }
}
